<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\User;

class HomeController extends Controller
{
    public function __construct()
    {
        $this->middleware('guest',[
           'only' => ['login']
        ]);
    }
    public function home()
    {
        return view('home.home');
    }

    public function create()
    {
        return view('users.register');
    }

    public function login()
    {
        return view('users.login');
    }

    public function loginIn(Request $request)
    {
        $c=$this->validate($request,[
            'email' => 'email|required',
            'password' => 'required'
        ]);
        if(Auth::attempt($c,$request->has('remember'))){
            if(Auth::user()->state) {
                session()->flash('success', '欢迎回来，阴阳师大人～');
                return redirect()->route('users.show', Auth::user());
            }else{
                Auth::logout();
                session()->flash('danger','大人请先激活哦～');
                return back();
            }

        }else{
            session()->flash('warning','邮箱密码不匹配哦～');
            return back();
        }
    }

    public function logout()
    {
        Auth::logout();
        session()->flash('success','欢迎下次再来哦～');
        return redirect()->route('login');
    }
}
